package net.oschina.arvin.admin.service.impl;

import net.oschina.arvin.admin.dao.UserDao;
import net.oschina.arvin.admin.model.User;
import net.oschina.arvin.admin.service.UserService;
import net.oschina.arvin.admin.util.JsonUtil;
import org.hibernate.jpa.criteria.predicate.LikePredicate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Arvin
 * @time 2017/11/22 22:23
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public User findById(Long id) {
        return userDao.getOne(id);
    }

    @Override
    public User findByUname(String uname) {

        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("uname", ExampleMatcher.GenericPropertyMatcher.of(ExampleMatcher.StringMatcher.CONTAINING));

        User user = new User();
        user.setUname(uname);

        Example<User> userExample = Example.of(user, matcher);

        Pageable pageable = new PageRequest(1, 10);

        Page<User> userPage = userDao.findAll(userExample, pageable);

        System.out.println(JsonUtil.toJson(userPage));

        System.out.println("-------------------");

        return userDao.findFirstByUname(uname);
    }

    @Override
    public Page<User> queryPage(Pageable pageable) {
        return userDao.findAll(pageable);
    }
}
